﻿<UserControl x:Class="MiniUML.Plugins.UmlClassDiagram.Resources.Shapes.GenericUmlAssociation"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:controls="clr-namespace:MiniUML.View.Controls;assembly=MiniUML.View"
             xmlns:views="clr-namespace:MiniUML.View.Views;assembly=MiniUML.View"
             xmlns:utilities="clr-namespace:MiniUML.View.Utilities;assembly=MiniUML.View"
             xmlns:local="clr-namespace:MiniUML.Plugins.UmlClassDiagram.Resources.Shapes"
             x:Name="_this"
             SnapsToDevicePixels="True"
             views:CanvasView.CustomDrag="True">

    <UserControl.Resources>
        
        <!-- Set the style of the ThreePieceLine's Polyline.-->
        <Style TargetType="Polyline">
            <Setter Property="Stroke" Value="{Binding ElementName='_this', Path='Stroke'}"/>
        </Style>
        
        <!-- Set the style of arrow heads (whose style must be {DynamicResource StrokeStyle}). -->
        <Style x:Key="StrokeStyle" TargetType="Shape">
            <Setter Property="Stroke" Value="{Binding ElementName='_this', Path='Stroke'}"/>
        </Style>

        <utilities:ArrowTemplateSelector x:Key="ArrowTemplateSelector"/>
        <utilities:ShapeIdToControlConverter x:Key="ShapeIdToControlConverter" />
        
    </UserControl.Resources>

    <Canvas x:Name="canvas">
        
        <local:ThreePieceLine
            FromX="{Binding Path='FirstNode.Attribute[Left].Value'}"
            FromY="{Binding Path='FirstNode.Attribute[Top].Value'}"
            FromOrientation="{Binding ElementName='FromAnchor', Path='SnapOrientation'}"
            ToX="{Binding Path='LastNode.Attribute[Left].Value'}"
            ToY="{Binding Path='LastNode.Attribute[Top].Value'}"
            ToOrientation="{Binding ElementName='ToAnchor', Path='SnapOrientation'}"
            x:Name="tpl"
            SnapTargetUpdate="snapTargetUpdate"/>

        <ContentControl ContentTemplateSelector="{StaticResource ArrowTemplateSelector}"
            Canvas.Left="{Binding Path=FirstNode.Attribute[Left].Value}"
            Canvas.Top="{Binding Path=FirstNode.Attribute[Top].Value}">
            <ContentControl.RenderTransform>
                <RotateTransform Angle="{Binding ElementName='tpl', Path=FromAngleInDegrees}"/>
            </ContentControl.RenderTransform>
            <Binding ElementName="_this" Path="FromArrow"/>
        </ContentControl>

        <ContentControl ContentTemplateSelector="{StaticResource ArrowTemplateSelector}"
            Canvas.Left="{Binding Path=LastNode.Attribute[Left].Value}"
            Canvas.Top="{Binding Path=LastNode.Attribute[Top].Value}">
            <ContentControl.RenderTransform>
                <RotateTransform Angle="{Binding ElementName='tpl', Path=ToAngleInDegrees}"/>
            </ContentControl.RenderTransform>
            <Binding ElementName="_this" Path="ToArrow"/>
        </ContentControl>

        <controls:AnchorPoint x:Name="FromAnchor"
                Canvas.Left="{Binding Path=FirstNode.Attribute[Left].Value}"
                Canvas.Top="{Binding Path=FirstNode.Attribute[Top].Value}"
                Left="{Binding Path=FirstNode.Attribute[Left].Value}"
                Top="{Binding Path=FirstNode.Attribute[Top].Value}"
                SnapTarget="{Binding Path=Attribute[From].Value, Converter={StaticResource ShapeIdToControlConverter}}">
        </controls:AnchorPoint>
        
        <controls:AnchorPoint x:Name="ToAnchor"
                Canvas.Left="{Binding Path=LastNode.Attribute[Left].Value}"
                Canvas.Top="{Binding Path=LastNode.Attribute[Top].Value}"
                Left="{Binding Path=LastNode.Attribute[Left].Value}"
                Top="{Binding Path=LastNode.Attribute[Top].Value}"
                SnapTarget="{Binding Path=Attribute[To].Value, Converter={StaticResource ShapeIdToControlConverter}}">
        </controls:AnchorPoint>
        
    </Canvas>
    
</UserControl>
